package com.atguigu.eduservice.service.impl;

import com.aiguigu.commonutils.ResultCode;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.dto.chapter.ChapterVo;
import com.atguigu.eduservice.entity.dto.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-24
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getCharterVideoById(String courseId) {

        // 1.根据课程id查出，章节 charter
        LambdaQueryWrapper<EduChapter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduChapter::getCourseId,courseId).orderBy(true,true,EduChapter::getSort);

        List<EduChapter> chapterList = this.list(wrapper);

        // 2.根据课程id查出，小结 video
        LambdaQueryWrapper<EduVideo> wrapperVideo = new LambdaQueryWrapper<>();
        wrapperVideo.eq(EduVideo::getCourseId,courseId).orderBy(true,true,EduVideo::getSort);
        List<EduVideo> videoList = eduVideoService.list(wrapperVideo);

        // 3.创建分装对象
        List<ChapterVo> chapterVoList = new ArrayList<>();
        // 4.遍历章节
        for (EduChapter eduChapter : chapterList) {
            // 5.章节封装对象
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(eduChapter.getId());
            chapterVo.setTitle(eduChapter.getTitle());
            // 6.小节分装对象
            List<VideoVo> eduVideos = new ArrayList<>();
            // 7.遍历小节
            for (EduVideo eduVideo : videoList) {
                // 8.该小节处于此大章节
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(eduVideo.getId());
                    videoVo.setTitle(eduVideo.getTitle());
                    videoVo.setIsFree(eduVideo.getIsFree());
                    videoVo.setVideoSourceId(eduVideo.getVideoSourceId());
                    eduVideos.add(videoVo);
                }
            }
            // 9.将小节set进章节的children
            chapterVo.setChildren(eduVideos);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    @Override
    public boolean deleteEntityById(String id) {
        // 查询章节下面是否有小结
        LambdaQueryWrapper<EduVideo> eduVideoWrapper = new LambdaQueryWrapper<>();
        eduVideoWrapper.eq(EduVideo::getChapterId, id);
       int count = eduVideoService.count(eduVideoWrapper);
        if(count >= 1){
            throw new GuLiException(ResultCode.ERROR, "该章节下有小节内容,不能进行删除");
        }else{
            return this.removeById(id);
        }
    }

    @Override
    public void deleteEntityByCourseId(String courseId) {
        // 删除章节
        LambdaQueryWrapper<EduChapter> eduVideoWrapper = new LambdaQueryWrapper();
        eduVideoWrapper.eq(EduChapter::getCourseId, courseId);
        this.remove(eduVideoWrapper);
    }
}
